package ckw.gui.wizards;

import java.util.ArrayList;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import ckw.database.VotingCard;

public class WzdElectionDlgVotingCardAddEdit extends Dialog {
	private ArrayList<VotingCard> votingCardList;
	private VotingCard votingCard;
	
	private Text txtNazwa;
	private Text txtOpis;

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 * @param votingCardList 
	 * @param votingCard 
	 */
	public WzdElectionDlgVotingCardAddEdit(Shell parentShell, ArrayList<VotingCard> votingCardList, VotingCard votingCard) {
		super(parentShell);
		
		this.votingCardList = votingCardList;
		this.votingCard = votingCard;
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(2, false));

		Label lblNazwa = new Label(container, SWT.NONE);
		lblNazwa.setText("Nazwa:");

		txtNazwa = new Text(container, SWT.BORDER);
		txtNazwa.setText(votingCard.getName() == null ? "" : votingCard.getName());
		txtNazwa.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label lblOpis = new Label(container, SWT.NONE);
		lblOpis.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1,
				1));
		lblOpis.setText("Opis:");

		txtOpis = new Text(container, SWT.BORDER | SWT.WRAP | SWT.MULTI);
		txtOpis.setText(votingCard.getDescription() == null ? "" : votingCard.getDescription());
		GridData gd_txtOpis = new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1);
		gd_txtOpis.heightHint = 95;
		txtOpis.setLayoutData(gd_txtOpis);
		
		return container;
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}

	@Override
	protected void okPressed() {
		votingCard.setName(txtNazwa.getText());
		votingCard.setDescription(txtOpis.getText());
		// Jesli nie zawiera, to dodajemy, jesli zawiera, po prostu edytujemy
		if(!votingCardList.contains(votingCard)) {
			votingCardList.add(votingCard);
		}
		
		super.okPressed();
	}
}
